package com.bootdo.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.service.DictService;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.CustomerDO;
import com.bootdo.crm.service.CustomerService;

/**
 * 客户表
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:28
 */
 
@Controller
@RequestMapping("/crm/customer")
public class CustomerController extends BaseController {
	@Autowired
	private CustomerService customerService;

	@Autowired
	private DictService dictService;
	
	@GetMapping()
	@RequiresPermissions("crm:customer:customer")
	String Customer(){
	    return "crm/customer/customer";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:customer:customer")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CustomerDO> customerList = customerService.list(query);
		int total = customerService.count(query);
		PageUtils pageUtils = new PageUtils(customerList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:customer:add")
	String add(Model model ){
		Map<String, Object> map = new HashMap<>(16);
		map.put("type","source");
		List<DictDO> customerSource = dictService.list(map);
		map.put("type","level");
		List<DictDO>customerLevel = dictService.list(map);
		map.put("type","industry");
		List<DictDO>customerIndustry = dictService.list(map);
		model.addAttribute("customerLevel", customerLevel );
		model.addAttribute("customerSource", customerSource );
		model.addAttribute("customerIndustry", customerIndustry );
	    return "crm/customer/add";
	}

	@GetMapping("/edit/{customerId}")
	@RequiresPermissions("crm:customer:edit")
	String edit(@PathVariable("customerId") Integer customerId,Model model){
		CustomerDO customer = customerService.get(customerId);
		model.addAttribute("customer", customer);
	    return "crm/customer/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:customer:add")
	public R save( CustomerDO customer){
		customer.setCreateUserId(ShiroUtils.getUserId());
		customer.setCreateTime(TimeUtils.getStringTime());
		if(customerService.save(customer)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:customer:edit")
	public R update( CustomerDO customer){
		customer.setUpdateTime(TimeUtils.getStringTime());
		customerService.update(customer);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:customer:remove")
	public R remove( Integer customerId){
		if(customerService.remove(customerId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:customer:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] customerIds){
		customerService.batchRemove(customerIds);
		return R.ok();
	}
	
}
